package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.CustomException;
import cn.wolfcode.p2p.base.mapper.UserInfoMapper;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.business.domain.UserBankInfo;
import cn.wolfcode.p2p.util.AssertUtil;
import cn.wolfcode.p2p.util.BitStatesUtils;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserInfoServiceImpl implements IUserInfoService {
        @Autowired
        private UserInfoMapper userInfoMapper;


        @Override
        public void init(LoginInfo loginInfo) {
                UserInfo userInfo = new UserInfo();
                userInfo.setId(loginInfo.getId());
                userInfo.setPhoneNumber(loginInfo.getUsername());
                userInfoMapper.insert(userInfo);
        }

        @Override
        public UserInfo select(Long id) {
                return userInfoMapper.selectByPrimaryKey(id);
        }

        @Override
        public void basicInfosave(UserInfo userInfo) {
                AssertUtil.instance().isNotNull(userInfo.getEducationBackground(),"不能为空");
                Long id = UserContext.getLoginInfo().getId();
                UserInfo oldUserInfo = userInfoMapper.selectByPrimaryKey(id);
                oldUserInfo.setEducationBackground(userInfo.getEducationBackground());
                oldUserInfo.setHouseCondition(userInfo.getHouseCondition());
                oldUserInfo.setIncomeGrade(userInfo.getIncomeGrade());
                oldUserInfo.setKidCount(userInfo.getKidCount());
                oldUserInfo.setMarriage(userInfo.getMarriage());

                if(!oldUserInfo.hasBasicInfo()){
                        //如果没有 通过,就改变认证状态
                        long tempState = BitStatesUtils.addState(oldUserInfo.getBitState(), BitStatesUtils.OP_BASIC_INFO);
                        oldUserInfo.setBitState(tempState);
                }
                update(oldUserInfo);


        }
        public void update(UserInfo userInfo){
                int count = userInfoMapper.updateByPrimaryKey(userInfo);
                if(count==0){
                        throw  new CustomException("用户信息修改失败!乐观锁异常,{"+userInfo.getId()+"}请联系客服!");
                }
        }

        @Override
        public void updateHostingPayPassWord(Long uid) {
                UserInfo userInfo = getById(uid);
                if(userInfo!=null){
                        userInfo.addBitState(BitStatesUtils.OP_HOSTING_PASSWORD);
                        update(userInfo);
                }
        }

        @Override
        public UserInfo getById(Long id) {
                return userInfoMapper.selectByPrimaryKey(id);
        }


}
